<?php
	include("initialize.php");
?>
<html>
<head>
	<style type="text/css">
		.schedule{
			border-style: solid;
			border-width: 1px;
			padding: 3px;
		}
		
		p, .pMirror{
			font-family: "Trebuchet MS", sans-serif;
			font-size: 13px;
			text-align: justify;
			color: #000000;
			margin-right: 8px;
		}
		
		input.noborder{
			border: none;
			border-color: #FFFFFF;
		}
		
		h1{
			font-family: Arial, Helvetica, sans-serif;
			font-size: 18px;
			color: #000000;
		}
	</style>
	
	<?php
		include ("write_functions.php");
		include("mark.php");
		
		connect("localhost", "root", "", "gradegriddatabase");
		
		$classid = $_GET["classid"];
		
		//Query for the major requirements
		$getreqs_query = sprintf("SELECT name, major_id FROM major WHERE template_id IN
		                         (SELECT template_id FROM class WHERE class_id = '%s') ORDER BY major_id",
								 $classid);
		$grq = mysql_query($getreqs_query);
		$majors_limit = mysql_num_rows($grq);
		
		//Query for the students
		$findstudents_query = sprintf("SELECT last_name, first_name, account_id FROM account WHERE account_id IN
		                               (SELECT account_id FROM take WHERE class_id = '%s') ORDER BY last_name, first_name",
									   $classid);
		$fsq = mysql_query($findstudents_query);
		$student_limit = mysql_num_rows($fsq);
	?>
	
</head>
<body style="padding: 0px; margin: 0px;">
	<?php
		$readonly = isset($_GET["printerfriendly"]);
		$account_query = sprintf("SELECT * FROM account WHERE username = '%s'",
	                             mysql_real_escape_string($_SESSION["username"]));
		$account = mysql_fetch_array(mysql_query($account_query));
	
		$class_query = sprintf("SELECT * FROM class WHERE class_id = '%s'",
	                           $classid);
		$class = mysql_fetch_array(mysql_query($class_query));
		$section = $class["class_name"];
		$courseid = $class["course_id"];
		
		$course_query = sprintf("SELECT * FROM course WHERE course_id = '%s'",
	                            mysql_real_escape_string($class["course_id"]));
		$course = mysql_fetch_array(mysql_query($course_query));
		$classname = $course["course_name"];
		
		$school_query = sprintf("SELECT * FROM school WHERE school.account_id = '%s' and school.school_id = '%s'",
	                            mysql_real_escape_string($account["account_id"]),
							    mysql_real_escape_string($course["school_id"]));
		$school = mysql_fetch_array(mysql_query($school_query));
		
		if($school["school_name"] != null){
			echo "<h1 style='color: #B4B4B4'>" . $school['school_name'] . "<br />";
			echo $school['college_name'] . "<br />";
			echo $school['department_name'] . "</h1>";
		}
	    
		if($account["first_name"] != null && $account["last_name"] != null){
			echo "<p>Instructor: " . $account["first_name"] . " " . $account["last_name"] . "<br />";
			echo "Class: $classname - $section</p>";
		} else{
			echo "<p>Class: $classname - $section</p>";
		}
	?>
	<?php
		echo "<form action='grades.php?classid=$classid' method='post' id='gradegrid'>\n";
		echo "<input type='textbox' name='gradegrid' style='display:none;' value='foo' />\n"; //Just a toggle field so we'll have means
		                                                                                      //to know if page was generated through form
																							  //submission.
		echo "<table class='schedule pMirror'>\n";
		$formnames = array();
		$minorids = array();
		$i = 0;
		$limit = mysql_num_rows($grq);
		$minlimit = 0;
		
		//Echo the student names in the table, alongside the minor requirements.
		//(See above.)
		echo "<tr>\n";
		echo "<td class='schedule'>Student Names</td>\n";
		while($i < $limit){
			mysql_data_seek($grq, $i);
			$requirements = mysql_fetch_array($grq);
			
			//...the minor requirements
			$getminors_query = sprintf("SELECT * FROM minor WHERE major_id = '%s' ORDER BY name, minor_id",
			                           mysql_real_escape_string($requirements["major_id"]));
			$gmq = mysql_query($getminors_query);
			$j = 0;
			$minors_limit = mysql_num_rows($gmq);
			$minlimit += $minors_limit;
			
			while($j < $minors_limit){
				mysql_data_seek($gmq, $j);
				$minors = mysql_fetch_array($gmq);
				echo "<td class='schedule'>" . $requirements["name"] . ": " . $minors["name"] . " (" . $minors["maxrawscore"] . ")" . "</td>\n";
				$fieldname = str_replace(" ", "", $requirements["name"]);
				array_push($formnames, $fieldname); //Take the names for the fields in the form (to be made unique
				                                    //by their matrix coordinates.)
				array_push($minorids, $minors["minor_id"]); //Take all the minorids associated with this record.
				$j++;
			}
			$i++;
		}
		echo "<td class='schedule'>Total</td>\n"; //For the total
		echo "<td class='schedule'>Mark</td>\n";
		echo "</tr>";
		
		$i = 0;
		$limit = mysql_num_rows($fsq);
		
		//Now output the fields for the grade
		while($i < $limit){
			mysql_data_seek($fsq, $i);
			$students = mysql_fetch_array($fsq);
			$accountid = mysql_real_escape_string($students["account_id"]);
			
			//Get the student grades
			$getgrades_query = sprintf("SELECT grade, major_id, minor_id FROM grade WHERE account_id='%s' and class_id IN
                                        (SELECT class_id FROM class, course WHERE class_id = '%s') ORDER BY major_id, minor_id",
			                           $accountid, $classid);
			
			$ggq = mysql_query($getgrades_query);
			$grades = mysql_fetch_array($ggq);
			$j = 0;
			$limit2 = mysql_num_rows($ggq);
			$namecounter = 0;
			$seek = true;
			
			echo "<tr>";
			echo "<td class='schedule'>". $students["last_name"] . ", " . $students["first_name"] . "</td>\n";
			
			/*
			Get all the available grades for the student. Since not all grades may be available
			for the moment, we still need to check if we are echo-ing under a column with an available
			grade. Note that this scheme relies heavily on the ordering of the queried tuples.
			*/
			while($j < $minlimit){
				if($j < $limit2 && $seek){
					mysql_data_seek($ggq, $j);
					$grades = mysql_fetch_array($ggq);
				}
				$name = $formnames[$namecounter] . $i . $j;
				
				if($grades["minor_id"] == $minorids[$namecounter]){ //If we are currently in the column for a recorded grade.
					echo "<td class='schedule'><input type='text' class='noborder' readonly='readonly' value='" . $grades["grade"] . "' name='" . $name . "' /></td>\n";
					$seek = true;
				} else{
					echo "<td class='schedule'><input type='text' class='noborder' readonly='readonly' value='' name='" . $name . "' /></td>\n";
					$seek = false;
				}
				$j++;
				$namecounter++;
			}
			
			//And another one for the total.
			//Query the student's total if it exists.
			$studtotal_qs = sprintf("SELECT totalgrade FROM total WHERE class_id = '%s' AND account_id = '%s'",
			                        $classid, $accountid);
			$studtotal_select_query = mysql_query($studtotal_qs);
			
			if($studtotal_select_query){
				$studtotal_array = mysql_fetch_array($studtotal_select_query);
				$totalgrade = $studtotal_array["totalgrade"];
				echo "<td class='schedule'><input type='text' class='noborder' value='$totalgrade' name='total' readonly='readonly' /></td>\n";
				
				$schemeid_query = mysql_query("SELECT schemeid FROM course WHERE course_id = '$courseid'");
				$schemeid_array = mysql_fetch_array($schemeid_query);
				$schemeid = $schemeid_array["schemeid"];
				$mark = mark($totalgrade, $schemeid);
				echo "<td class='schedule'><input type='text' class='noborder' value='$mark' readonly='readonly' /></td>\n";
				
			} else{
				echo "<td class='schedule'><input type='text' class='noborder' name='total' readonly='readonly' /></td>\n";
			}
			echo "</tr>";
			
			$i++;
		}
		echo "</table></form>";
	?>
	<?php
		//And now for some more printer-friendly effects.
		$deanset = $school["dean"] != null;
		$secretaryset = $school["college_secretary"] != null;
		$collegeset = $school["college_name"] != null;
		$deptchairset = $school["department_chair"] != null;
		$deptnameset = $school["department_name"] != null;
		
		if($deanset){
			echo "<p>" . $school["dean"] . "<br />";
			echo "Dean";
			
			if($collegeset){
				echo ", " . $school["college_name"];
			}
			
			echo "</p>";
		}
		
		if($secretaryset){
			echo "<p>" . $school["college_secretary"] . "<br />";
			echo "College Secretary";
			
			if($collegeset){
				echo ", " . $school["college_name"];
			}
			
			echo "</p>";
		}
		
		if($deptchairset){
			echo "<p>" . $school["department_chair"] . "<br />";
			echo "Department Chair";
			
			if($deptnameset){
				echo ", " . $school["department_name"] . "<br />";
			}
			
			echo "</p>";
		}
	?>
</body>
</html>